I tried using Docker for the first time

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.

Docker installation

You can install Docker with the command below.


$ apt install docker docker-compose

Image related

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

Start / Stop / Delete Container

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

Operation on a running container

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

Options when starting the container

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

at the end

I've researched various things and finally made a simple page, but it seems that there is still a long way to go.

Recommended Posts

I tried using Docker for the first time
I tried touching Docker for the first time
[Rails] I tried using the button_to method for the first time
I tried the Docker tutorial!
I tried using the Migration Toolkit for Application Binaries
I tried using Docker Desktop for Windows on Windows 10 Home
Command to try using Docker for the time being
Spring Boot for the first time
Spring AOP for the first time
I tried to build the environment little by little using docker
Introduction to java for the first time # 2
I tried using Scalar DL with Docker
Learning for the first time java [Introduction]
Java14 came out, so I tried record for the time being
[For beginners] I tried using DBUnit in Eclipse
Java12 came out, so I tried the switch expression for the time being
I tried using Gson
[For beginners] I tried using JUnit 5 in Eclipse
Walls hit by Rspec for the first time
I tried using TestNG
I tried using Galasa
[API] I tried using the zip code search API
Android Studio development for the first time (for beginners)
I tried using the profiler of IntelliJ IDEA
The story of intentionally using try catch for the first time in my life
Impressions and doubts about using java for the first time in Android Studio
I tried using the Server Push function of Servlet 4.0
I tried to summarize the state transition of docker
I tried installing the Docker Integration plugin in IntelliJ
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
I tried using an extended for statement in Java
Learning memo when learning Java for the first time (personal learning memo)
[WIP] I tried the configuration of Docker + Streama + NFS
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
I tried to build an environment using Docker (beginner)
Ideal and reality that I felt when I used Optional for the first time ~ Implementation of cache using Map ~
I tried using azure cloud-init
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried using Java REPL
I tried the FizzBuzz problem
I tried BIND with Docker
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried using the GitHub repository as a library server
I tried to build the environment of WSL2 + Docker + VSCode
I tried using the CameraX library with Android Java Fragment
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)
Modeling a Digimon with DDD for the first time Part 1
docker What should I do first? (Somehow I understand the shell, but ... for beginners at the level)
I tried using anakia + Jing now
I tried using Spring + Mybatis + DbUnit
I tried using JOOQ with Gradle
I tried to explain the method
I tried the Java framework "Quarkus"
[Rails] I tried deleting the application
I tried using Java8 Stream API
I tried using JWT in Java
[Android] I tried using Coordinator Layout.