How to use Docker Container
In the last post, TipsMake.com showed you how to install Docker. In this tutorial, we will explore the most used Docker commands.
Instructions for using Docker Container
- How to learn more about Docker commands
- How to find and drag a Docker image
- How to run and stop a Container Docker?
- How to customize the Docker Container
- How to delete Docker and image?
How to learn more about Docker commands
Open a terminal and enter:
docker
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. For 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
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.
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
For 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
Usually, 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
For 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
Now, you can edit 'index.html' and create a home page for the website, for 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
The container will continue to function until you stop it and remove all containers that depend on that image.
Docker has evolved into a rather complex project. But you can slowly 'dig' into every command by referring to the instructions page. For example, man docker run will show everything about command parameters running docker, such as how to set the preferred IP address for each container or how to limit memory usage. Gradually you can master each Docker command if you are patient.
Good luck!
See more:
- How to safely check desktop applications with Docker
- 5 tips to learn Docker effectively for beginners
- What is Container Linux? Should I use it?
You should read it
- How to safely check desktop applications with Docker
- 6 reasons to use Docker virtualization software
- Docker best practices you need to know
- Docker Hub is used by hackers to spread Cryptojacking malware
- How to install Docker in Linux
- 10 Best Docker Alternatives 2022
- How to Containerize a Nest.js Application Using Docker and Docker Compose
- Common commands in Docker
- How to run Docker on Raspberry Pi
- 5 useful tips to learn Docker in 2018
- Containerize Go App with Docker
- 5 tips to learn Docker effectively for beginners
Maybe you are interested
10 Best Docker Alternatives 2023
How to create an effective Docker image for a Python project
How to Install Apache Guacamole via Docker on Ubuntu 22.04
Docker best practices you need to know
How to Containerize a Nest.js Application Using Docker and Docker Compose
How to containerize a Rust app with Docker