Running Docker Images
To create a basic Docker container from the Debian image, we run the following:
What's happening here? We invoke the docker
command, and tell it to run
a command in a container created using the debian
image, which it gets by default from Dockerhub. The rest of this line simply tells Docker what command to run in the container, in this case echo 'Hello, World!'
.
Important note: Docker images are built for specific CPU architectures (i.e. amd64 vs arm64) and you can only run an image if its the same architecture as your computer. Many popular Docker images have versions for both amd64 and arm64, but it's up to you to check whether or not a version compatible with your CPU's architecture exists before trying to run an image.
We can explore this container a bit more by creating an interactive session inside of it. This allows us to see the filesystem present in the container.
To start an interactive session in the container created using the ubuntu image:
Inside the container, we can't do very much, but we can see that it has its own filesystem with the usual FHS layout. We can certainly make changes to this filesystem similarly to how we do so on a normal Unix system. However, once the container stops running, any changes we make are reset once the container comes back up.
If data inside the container does not survive container reboots, how does any data persist?
There are two ways that Docker allows us to persist data: bind mounting the host's filesystem or creating a Docker volume.
We'll focus on bind mounting first. Bind mounting essentially lets you plug a hole in a Docker container filesystem that points to somewhere on your host computer's filesystem.
To bind mount a directory, when we run our container, we pass the -v
flag then provide <source>:<destination>
where <source>
is the directory that you want accessed in the container and <destination>
is where in the container the directory points to.
For example, to bind mount a local directory in the Ubuntu container
Now, if you create something in /mnt
in the container, you'll see those changes made on your local directory as well.
This Ubuntu images is pretty barebones, as you've seen. Images such as this aren't meant for using outright, but instead for building upon to create other, more useful images.
We'll use one of these more useful images to set up an R development environment.
The image we'll be using is rocker/rstudio
, an image made by the R community for setting up a barebones R environemnt or for building a more robust R environment.
Let's start up an interactive session using the rocker/rstudio
image available on Dockerhub.
As you can see, rocker/rstudio
does not come with tidyverse
built-in. However, the R environment it provides is just like any other R environment, so it's incredibly simple to install it.
Working with R in the command line can be fairly cumbersome. The real power of rocker/rstudio
is that it comes built-in with an RStudio server.
By default, RStudio binds to the port 8787. However, the port in the container is in its own network. So like before, we'll need to port forward from the container to our local network. Thankfully, Docker has a built-in way of doing this, using the -p
flag, which is supplied with <host port>:<container port>
, where host port is the port on your own computer and container port is the port in the container. To keep things simple, we'll keep the port numbers the same.
This should start an RStudio server which you can access on your computer's web browser at http://localhost:8787.
Remember, any files you create in this RStudio Server are created in the Docker container. When the Docker container stops, those files will be gone. If you want to save your files or use R files you have from previous work, it's best to bind mount the directory with your files and make sure to only make changes to the bind-mounted directory in the container.
Last updated