I started studying Docker. Make a note of this knowledge so that you do not forget it. This post is written on the assumption that it will work on Ubuntu 20.04.
You can install Docker with the command below.
$ apt install docker docker-compose
You can download images created by others from Docker Hub. This eliminates the need to make it yourself.
The name written in this
NAME field is the image name.
$ docker search nginx #Find nginx $ docker search -f is-official=true nginx #When searching only the official NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 14228 [OK]
For the name entered here, use the previous image name.
$ docker image pull nginx #image(nginx)Download
$ docker image ls
$ docker image rm nginx #Erase nginx image $ docker image prune -f #Erase all unused images
After downloading the image, create a container from that image.
Create and run a container.
--rm deletes the container as soon as it exits.
--it ~~ bash executes bash immediately after executing the container and accepts keyboard input.
--name names the container.
-d keeps the container running in the background.
$ docker container run --rm -it nginx bash #Create / execute a container, enter the container with bash, and destroy the container after bashing. $ docker container run --name websv -d nginx #Keep running under the container name websv in the background
Search for the created container.
$ docker container ls #Show only currently running containers $ docker container ls -a #Also show stopped containers
$ docker container stop websv
$ docker container restart websv
$ docker container rm websv #Delete the container name websv $ docker container prune -f #Delete all stopped containers
Operate on a container running in the background.
$ docker container exec -it websv bash #Enter the container websv with bash $ docker container exec websv bash -c "echo 'Hello'" #Execute echo command in container websv
In the official nginx image, the access log is printed to standard output. (There is no need to retrieve data from the container.)
$ docker container logs websv
There are many useful container startup options.
You can use
-p to port forward incoming communication to the host and pass it to the container. In the example below, the communication that came to TCP8080 on the host is forwarded to TCP80 on the container. If you want to receive UDP, write
-p 514: 514/udp. (Example of syslog: UDP514)
$ docker container run --name websv -d -p 8080:80 nginx
Mount the files and folders on the host into the container. In the example below, the host
/home/hoge/index.html replaces the container
/usr/share/nginx/html/index.html. By adding
readonly, writing from the container side to
index.html on the host is prohibited.
$ docker container run -d --name websv -p 8080:80 --mount type=bind,src=/home/hoge/index.html,dst=/usr/share/nginx/html/index.html,readonly nginx
I've researched various things and finally made a simple page, but it seems that there is still a long way to go.