Skip to main content
Skip table of contents

Step 3 of 4: Interacting with Docker Containers

Goal

In this step, you will run a Docker container in interactive mode and operate on shared files from within that container. You will also learn how to restart and reattach an exited container.

 

Sharing local files into a container

In the previous section, we used the run command with the busybox image. Busybox is very small and quick, but it comes with a limited set of software. Let's try to do something more interesting. We will use the Ubuntu image that we loaded from the tar archive.

BASH
xnat@xnat-31:~$ docker run -ti -v /data/xnat/home/logs/:/shared_logs ubuntu /bin/bash

In this example:

  • docker run -ti runs a container in interactive mode. This means the container does not simply run a command and finish, it continues running and lets you type additional commands.

  • -v /data/xnat/home/logs/:/shared_logs  shares your XNAT VM's logs folder into your Docker container. We will explore this concept more below.
  • xnat/ubuntu is the image you run. We loaded this image in the previous step.

  • /bin/bash is the command to run inside the new container. It opens up a new command prompt for you.

We should now see an interactive command line in our running container, something like

BASH
root@<container id>:/#

The "<container id>" the container's randomly generated id. Everyone's value for this will be different.

The argument we gave to docker to run (-v /data/xnat/home/logs:/shared_logs) created a directory inside our container. This is a special directory that has the /data/xnat/home/logs directory on the XNAT VM shared into the container as /shared_logs. Verify that you can see the logs inside your container.

BASH
root@<container id>:/# ls shared_logs
access.log       axis.log           launch.log      received.log   security.log  velocity.log
application.log  configuration.log  orm.log         remote.log     spring.log    xapi.log
automation.log   dicom.log          pipeline.log    restlet.log    sql.log       xdat.log
avalon.log       jms.log            prearchive.log  scheduler.log  turbine.log   xnatfs.log

Create a new tar file within this shared folder:

BASH
root@fa58978789b6:/# tar -czf shared_logs/logs.tar.gz shared_logs/*.log

If you aren't familiar with tar, it creates archive files similar to .zip files. This command made an archive of all the log files.

Exit the docker container.

BASH
root@fa58978789b6:/# exit

 

Now your container has exited. Back at the XNAT VM command line, you can see our newly minted tar.gz file.

BASH
xnat@xnat-31:~$ ls -l /data/xnat/home/logs/*.gz
-rw-r--r-- 1 root root 3363 Jun  5 19:06 /data/xnat/home/logs/logs.tar.gz

 

Extra: Restarting containers

If you are ready to move on to the next section, about building your own docker images, you can do so. Or if you want, here is an additional exercise on restarting containers.

 

Let us look at all our docker containers. Run the following command to see all the containers:

BASH
xnat@xnat-31:~$ docker ps -a
CONTAINER ID        IMAGE          COMMAND             CREATED             STATUS                     PORTS               NAMES
3524fab160e5        ubuntu         "/bin/bash"         12 minutes ago      Exited (0) 5 minutes ago                       nostalgic_jennings

By default, docker ps shows only running containers; with the -a flag it will also show stopped and exited containers. You may see more entries in your output than in the example above. For each container, the output shows the image it was based upon, the command you ran inside, and the randomly generated name docker assigned to it.

Docker container names

The names Docker assigns the containers can be pretty funny and strange. They combine entries randomly selected from a list of adjectives and a list of scientists. You can see the lists here.

 

We can start a container again using docker start <container id>. Look through the output of docker ps -a, and find the id for the container we just ran. (Or you can look back through the history of the commands we ran before, and extract the container id from the "root@<container id>:/#" command prompts we saw.) Then use that container id value in the following command:

BASH
xnat@xnat-31:~$ docker start -i <container id>
root@<container id>:/#

That puts you back at a command prompt inside the container we were running before. If we had just executed $ docker run ... ubuntu like we did to start this container the first time, it would have made a new container rather than restarting the old one.

Restarting an exited container can be very useful for debugging a process that failed, or examining logs.  Note that containers can be referenced using CONTAINER ID or NAME.  If you haven't assigned a name, Docker generates one for you, i.e. nostalgic_jennings.

 

Exit the Docker container command line to get back to your XNAT VM.  Next we'll learn how to build our own Docker images.

Step 4: Build docker images

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.