How to Use Docker Containers

docker-featured

In a previous tutorial we showed you how to install Docker. In this tutorial we will explore the most used Docker commands.

How to Learn More About Docker Commands

Open a terminal emulator and type:

docker

docker-command

This gives you a quick overview of what arguments are accepted by the docker command and what they do. Scroll up to see them all. You can observe that docker cp would “Copy files/folders between a container and the local filesystem.” But that’s not enough information. When you want to find out more about a specific sub-command, just add --help at the end. Example:

docker cp --help

How to Find and Pull a Docker Image

At first, images might be confused with containers themselves. Images are the base from which a container starts. It can then be changed (the container) in any way necessary. So you can have an image such as “httpd” and spin up containers “website1” and “website2.”

To search for an image, you can use a command like:

docker search apache

docker-search

You can also search on Docker Hub if you prefer to use your web browser.

To copy the image you require:

docker pull httpd

Obviously, you would replace “httpd” with the name of the image you need.

How to Run and Stop a Docker Container

To create a container out of this image and run it, type:

docker run -d --name=http-server --publish 80:80 httpd
  • -d runs the container in the background, detaching from its output.
  • --name specifies how you want to name your container.
  • --publish hostPort:containerPort publishes the port in the container to your host system. Apache serves requests on port 80 but only inside the container (isolated). With the command parameter used above, requests to port 80 on your host system will be directed to port 80 in the container, essentially giving you a pathway to reach inside the container. This means if you now open a browser on the host system and type “localhost” in the address bar, you would connect to the webserver that runs in your container.

docker-browse-localhost

To view what containers currently run:

docker ps

To view the containers that are currently shutdown:

docker ps -a

To shut down the container, type docker stop name-of-container. For example:

docker stop http-server

When you want to start the container again:

docker start http-server

And if you want to create another container from the Apache image:

docker run -d --name=http-server2 --publish 8080:80 httpd

Notice this time that port 8080 was used instead of 80. That’s so it doesn’t conflict with the other container. To access this one, you would enter localhost:8080 in your web browser.

How to Customize a Docker Container

Often, you will need to copy files to your container. To copy from host system to container, the syntax of the command is docker cp /path/to/local/file/or/directory name-of-container:/path/to/container/directory/or/file.

For example:

docker cp /bin/ls http-server:/tmp

To copy from container to host, use docker cp name-of-container:/path/to/container/file/or/directory /path/to/local/file/or/directory.

docker cp http-server:/etc /tmp

Sometimes you will also need to “step into” containers by opening a shell inside them. This way you can edit files, install additional binaries and customize them according to your needs.

docker exec -it http-server /bin/bash

docker-shell-in-container

Now, you could, for example, edit “index.html” and create a homepage for the website within.

To exit the shell in the container:

exit

How to Delete Docker Containers and Images

Before you delete a container, you must first stop it:

docker stop http-server2

Now, you can remove the container with:

docker rm http-server2

But it’s actually the images that take up more disk space than containers, usually. You can remove them with:

docker rmi httpd

docker-remove-image

This won’t work until you stop and remove all containers that depend on that image.

Conclusion

Docker has developed into quite a complex project. But you can slowly dig into every command by consulting the manual pages. For example: man docker run will show you everything about docker run command parameters, like how to set a preferred IP address for each container or how to limit memory usage. Slowly, but surely, you can master each Docker command by reading through the manual pages.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox