Maintain and Manipulate Docker Containers


Knowing how to create a dockerfile and build a docker image using that dockerfile, we can move ahead and dive deep into more advanced ways to manipulate docker containers. When we talk about manipulating docker containers, we include running, listing, restarting, cleaning up the dangling containers, running containers in interactive and detached modes, creating containers using executable images, executing commands inside docker containers using exec command and starting a bash inside a container, accessing logs from a docker container and killing or stopping a docker container.

Without any further ado, let’s dive deep into manipulating docker containers.

  • Running docker containers

After you have built an image using docker build command, you can run a container associated to that image using the docker run command.

sudo docker run <image−name>

This command simply runs an instance of the specified docker image. If you have already created an image before and you want to run another instance of that image, you can use the docker create command to create another container.

sudo docker create <image−name>

If you have already created a docker container before and you want to run that container now, you need to have it’s Id with you. To find out the Id of the container, you can use the following command −

sudo docker ps −a

Once you have the Id of the container, you can start the container using the following command −

sudo docker start <container−id>

If you want the container to launch a bash when you start it, you will have to use the attach flag with the docker start command.

sudo docker start −a <container−id>
  • Listing all the docker containers

If you want to display a list of all the docker containers, you can use the following command.

sudo docker ps −a

The above command displays the container Id, associated image name, date of creation, status, etc. of all the docker containers in your local machine.

  • Cleaning up all the dangling containers

To clean up all the exited containers, you need to get the Id of the containers whose status is exited. You can do so using the docker ps -a command discussed above. After you have the container Id, you can use the following command to remove the container −

sudo docker rm <container−id>

If you want to remove all docker objects from the system, you can use the prune command.

sudo docker system prune
  • Restarting a docker container

Restarting a docker container first kills the container if it's already running and then starts it back. It’s different from the start command because the start command is used to start containers that are not currently running.

sudo docker restart <container−id>
  • Running docker containers in interactive mode

You can launch a docker container bash in an interactive mode and run commands inside that bash in the same way you would run a command in the terminal of your host machine using the interactive mode. If you are launching a python container in interactive mode, you can use the python shell and if you are launching an ubuntu container in interactive mode, you can use the ubuntu terminal.

sudo docker run −it ubuntu
  • Running containers of an executable image

If your docker image is executable, that is it has a CMD or an ENTRYPOINT instruction associated with it, you can pass arguments in the docker run command to get it executed when the docker container starts.

sudo docker run <image−name> <args>

Example, to list all the files in an ubuntu container ‘s root directory, you can use the following command −

sudo docker run ubuntu ls
  • Running docker containers in detached mode

Running a docker container in detached mode, let’s it run in the background even if you close the terminal. You can use the −d flag to achieve this.

sudo docker run −d ubuntu
  • Launching bash of a docker container

To launch the bash of a docker container, so that you can execute commands inside the container to install packages or manipulate the file system, you can use the following command −

sudo docker run −it <image−name> /bin/bash

Or

sudo docker exec −it <container−id> sh
  • Executing commands directly inside a container

You can also use the docker exec command, to execute specified commands directly inside the docker container.

sudo docker exec <container−id> <command>
sudo docker exec −it <container−id> /bin/bash
  • Stopping/killing a docker container

To stop a container gracefully, you can use the docker stop command. It sends a signal called SIGTERM first, if not stopped sends the SIGKILL signal.

sudo docker stop <container−id>

To stop a container directly or kill it, use the following command −

sudo docker kill <container−id>

To conclude, in this article, we have seen almost all the important commands that you will need to maintain and manipulate docker containers seamlessly.

Updated on: 27-Oct-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements