Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Use Docker Container

Learn how to use Docker Container with clear steps, practical examples, and troubleshooting tips for safer, more reliable results.

Table of Contents

How to Use Docker Container is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

In the last post, TipsMake.com 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 and enter:

 docker 

How to Use Docker Container example image 1

This provides a quick overview of the arguments accepted by the docker command and what they do. Scroll up to see all. You can observe that cp docker will copy files / folders between a container and the local file system. But that is not enough. If you want to learn more about a specific sub-command, just add --help at the end. As an example,:

 docker cp --help 

How to find and drag a Docker image

At first, images can be confused with the container itself. Image is the base from which a container begins. It can then be changed (container) in any way necessary. Therefore, there may be an image such as 'httpd' and two containers 'website1' and 'website2'.

To search for an image, you can use the following command:

 docker search apache 

How to Use Docker Container example image 2

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

To copy the desired image:

 docker pull httpd 

Replace 'httpd' with the name of the image you need.

How to run and stop a Container Docker?

To create a container from this image and run it, enter:

 docker run -d --name=http-server --publish 80:80 httpd 
  • -d runs the container in the background, separated from its output.
  • --name specifies how you want to name your container.
  • --publish hostPort: containerPort exports the port in the container to your server system. Apache serves requests on port 80 but only inside containers (isolated). With the command parameter used above, requests to port 80 on the server system will be routed to port 80 in the container, essentially providing you with a path to reach inside the container. This means that when opening a browser on the server system and entering 'localhost' in the address bar, you will connect to the web server running in your container.

How to Use Docker Container example image 3

To see which containers are currently running, enter:

 docker ps 

To see the containers are currently inactive, enter:

 docker ps -a 

To turn off the container, enter:

 docker stop name-of-container 

As an example,:

 docker stop http-server 

When you want to restart the container:

 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 

Note that this time port 8080 was used instead of 80. That makes it not in conflict with another container. To access this container, enter localhost: 8080 in the web browser.

How to customize the Docker Container

typically, you will need to copy files to your container. To copy from the server system to the container, the syntax of the command is:

 docker cp /path/to/local/file/or/directory name-of-container:/path/to/container/directory/or/file 

As an example,:

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

To copy from container to server, 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 need to 'step in' the containers by opening a shell inside them. So you can edit files, install additional binaries and customize them according to your needs.

 docker exec -it http-server /bin/bash 

How to Use Docker Container example image 4

Now, you can edit 'index.html' and create a home page for the website, As an example,.

To exit the shell in the container:

 exit 

How to delete Docker and image?

Before deleting a container, first stop it:

 docker stop http-server2 

Now, can remove the container with the command:

 docker rm http-server2 

But it's really images that take up more disk space than usual. You can delete them by:

 docker rmi httpd 

How to Use Docker Container example image 5

The container will continue to function until you stop it and remove all containers that depend on that image.

  • How to safely check desktop applications with Docker
  • 5 tips to learn Docker effectively for beginners
  • What is Container Linux? Should I use it?

FAQ

How to find and drag a Docker image?

At first, images can be confused with the container itself. Image is the base from which a container begins. It can then be changed (container) in any way necessary. Therefore, there may be an image such as 'httpd' and two containers 'website1' and 'website2'.

How to run and stop a Container Docker?

To create a container from this image and run it, enter:

How to customize the Docker Container?

Typically, you will need to copy files to your container. To copy from the server system to the container, the syntax of the command is:

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.