Summary of Docker understanding by beginners ② ~ docker-compose ~


I've finally started learning Docker, so I'll summarize my understanding.

Note what you learned today

Launch a container that runs express in node.js. I did just this with docker-compose.

スクリーンショット 2020-10-22 11.29.14.png

First, prepare locally

cd ~
mkdir sample && cd sample
yarn init -y
yarn add express
touch index.js


const express = require('express');
const app = express();
app.get('/', (req, res) => {

app.listen(3000, () => {
  console.log('Listening on port 3000');
node index.js

Of course, if you hit the curl command locally, you'll get a reply.

curl http://localhost:3000

For the time being, I will plot the current situation. For the time being. スクリーンショット 2020-10-22 11.49.29.png

Preparing the Dockerfile

To create a docker image of node.js see here. This time is copy. Create the following Dockerfile directly under the sample directory.


FROM node:alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available ([email protected]+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .
# package.I don't want to do npm install after copying json every time I build.
#Therefore, after finishing the above, COPY. .Do it.
#Then the build will be executed only for the part where the code is changed.

CMD [ "node", "index.js" ]

And since it is painful if node_modules is also copied, create .dockerignore as well.

touch .dockerignore
echo 'node_modules' >> .dockerignore

By the way, if you come this far, you don't have to use docker-compose separately.

docker build -t app .
docker run -p 3000:3000 app

If you do, the container will start up. This time, instead of using the above command, launch the container with docker-compose.

Preparing docker-compose.yml

Create the following docker-compose.yml directly under the sample directory.


version: '3'
#Specify 3 without thinking about anything

#Describe information about the container. By writing multiple container information here, you can start multiple containers at the same time.
  #Launch a container called app (with DNS namespace).
    #Information at build time is described here. If you have a Dockerfile, you can refer to it, otherwise write it directly here.
      context: .
      #This docker at build time-compose.Same directory as yml (.) Refer to the Dockerfile.
      #If there are multiple Dockerfiles, it is necessary to specify the directory separately in the context, or specify the name as docokerfile: XXX.
    container_name: express-app
    #Give it an arbitrary container name. You can use this container name to access the container with the following command
    # docker exec -it express-app sh
      - '3000:3000'
    # docker run -p 3000:It has the same meaning as 3000.
      - './:usr/src/app'
    #Current directory of local PC (./) And the container~/usr/src/Synchronize the app directory
    #Also convenient for development.

docker-compose up and finish

docker-compose up will launch the container. If you type the curl command from your local PC, you will get a reply from the container. As shown in the first figure.

curl http://localhost:3000

スクリーンショット 2020-10-22 13.30.27.png

Do docker-compose down to stop the container. If you rewrite the contents of ʻindex.js, rebuild it with docker-compose build`.

Also, if you do docker-compose up -d, the container will start up in the background, but at this time it may be difficult to see the console.log in the container, so I like the foreground.


I have summarized the basics during the basics. Thank you very much.

Recommended Posts

Summary of Docker understanding by beginners ② ~ docker-compose ~
Summary of Docker understanding by beginners ① ~ docker run -p ~
Summary of Docker understanding by beginners ③ ~ Until proxying API using nginx ~
Summary of Docker understanding by beginners ⑤ ~ Until deploying docker container on EC2 instance ~
Summary of Docker understanding by beginners ④ ~ Until EC2 instance is started and docker is installed ~
[Java] Beginner's understanding of Servlet-②
[Java] Beginner's understanding of Servlet-①
Object-oriented summary by beginners (Java)
[For beginners] Summary of java constructor
[Rails] Introduction of Rubocop by beginners
Summary of frequently used Docker commands
The process of understanding Gemfile by non-engineers
Docker basic summary
A brief summary of Bootstrap features for beginners
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
Summary of basic knowledge of Rails acquired by progate
[Memo] docker summary
Docker command summary
[Docker] Command summary
Summary of frequently used commands in Rails and Docker
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Weight reduction of Docker image by multi-stage build-React application
Summary of revisions (new era) support by Java version
Introduction of Docker --Part 1--
Summary of OpenJDK sources
Summary of strong parameters
Summary of jar files
List of beginners (List) memo
Summary of information security
Summary of using FragmentArgs
Docker Compact Manual (3: docker-compose)
docker command personal summary
Summary of using DBFlow
Summary of Java support 2018
List of beginners (List) memo
[Java] Beginner's understanding of Servlet-②
[Java] Beginner's understanding of Servlet-①
[For beginners] Summary of java constructor
[Rails] Introduction of Rubocop by beginners
Summary of Docker understanding by beginners ② ~ docker-compose ~
The story of RxJava suffering from NoSuchElementException
Rails [For beginners] Implementation of comment function
Explanation of Ruby on rails for beginners ①
Install by specifying the version of Django in the Docker environment
Ruby on Rails for beginners! !! Summary of new posting functions
Get only the ID of the container specified by docker ps
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
Recommendation of set operation by Java (and understanding of equals and hashCode)
[Summary of technical books] Summary of reading "Learn Docker from the basics"
Summary of values returned by the Spliterator characteristics method #java