This article is part of the Docker series:
- How to Install Docker in Linux
- How to Use Docker Containers
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:
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:
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:
You can also search on Docker Hub if you prefer to use your web browser.
To copy the image you require:
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:
-druns the container in the background, detaching from its output.
--namespecifies how you want to name your container.
--publish hostPort:containerPortpublishes 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.
To view what containers currently run:
To view the containers that are currently shutdown:
To shut down the container, type
docker stop name-of-container. For example:
When you want to start the container again:
And if you want to create another container from the Apache image:
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.
To copy from container to host, use
docker cp name-of-container:/path/to/container/file/or/directory /path/to/local/file/or/directory.
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.
Now, you could, for example, edit “index.html” and create a homepage for the website within.
To exit the shell in the container:
How to Delete Docker Containers and Images
Before you delete a container, you must first stop it:
Now, you can remove the container with:
But it’s actually the images that take up more disk space than containers, usually. You can remove them with:
This won’t work until you stop and remove all containers that depend on that image.
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.