try docker-compose

Try docker-compose.

Simple docker-compose configuration to launch nginx

FROM centos:centos8

RUN dnf install -y nginx

CMD nginx -g 'daemon off;'

docker-compose.yml


version: '3'
services:
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - ./hoge:/hoge

basic operation

//Start-up
$ docker-compose up -d
Creating network "docker-compose-test_default" with the default driver
Creating docker-compose-test_web_1 ... done

//Container confirmation
$ docker-compose ps 
          Name                         Command               State          Ports        
-----------------------------------------------------------------------------------------
docker-compose-test_web_1   /bin/sh -c nginx -g 'daemo ...   Up      0.0.0.0:8000->80/tcp

//Stop
$ docker-compose stop 
Stopping docker-compose-test_web_1 ... done

//Stop&Delete
$ docker-compose down
Removing docker-compose-test_web_1 ... done
Removing network docker-compose-test_default

$ docker-compose ps
Name   Command   State   Ports
------------------------------

Project name

docker-compose has the concept of a project, and you can give it a project name. The container name seen by the docker-compose ps command was docker-compose-test_web_1, but the container name is the project name + service name. The project name defaults to the directory name where docker-compose.yml is located. To specify the project name, use the -p option or define it in the COMPOSE_PROJECT_NAME environment variable.

// -Specify with p.-Be careful where you put p. After up-docker instead of p-Do immediately after compose
$ docker-compose -p hogeee up -d
Creating network "hogeee_default" with the default driver
Building web
Step 1/3 : FROM centos:centos8
 ---> 0d120b6ccaa8
Step 2/3 : RUN dnf install -y nginx
 ---> Using cache
 ---> fc9418763f55
Step 3/3 : CMD nginx -g 'daemon off;'
 ---> Using cache
 ---> 6d41d3e27b18

Successfully built 6d41d3e27b18
Successfully tagged hogeee_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hogeee_web_1 ... done


//ps confirmation. every time-Note that you need to add p
$ docker-compose -p hogeee ps
    Name                  Command               State          Ports        
----------------------------------------------------------------------------
hogeee_web_1   /bin/sh -c nginx -g 'daemo ...   Up      0.0.0.0:8000->80/tcp

//Stop&Delete
$ docker-compose -p hogeee down 
Stopping hogeee_web_1 ... done
Removing hogeee_web_1 ... done
Removing network hogeee_default


//Specify with an environment variable.
$ COMPOSE_PROJECT_NAME=fuga docker-compose up -d
Creating network "fuga_default" with the default driver
Building web
Step 1/3 : FROM centos:centos8
 ---> 0d120b6ccaa8
Step 2/3 : RUN dnf install -y nginx
 ---> Using cache
 ---> fc9418763f55
Step 3/3 : CMD nginx -g 'daemon off;'
 ---> Using cache
 ---> 6d41d3e27b18

Successfully built 6d41d3e27b18
Successfully tagged fuga_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating fuga_web_1 ... done

$ COMPOSE_PROJECT_NAME=fuga docker-compose ps 
   Name                 Command               State          Ports        
--------------------------------------------------------------------------
fuga_web_1   /bin/sh -c nginx -g 'daemo ...   Up      0.0.0.0:8000->80/tcp

Specify image in docker-compose.yml

version: '3'
services:
    centos8:
        image: centos:centos8
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - ./hoge:/hoge

Start order control by depends_on

You can control the boot order with depends_on. You can control the startup order, but since the next startup starts without waiting for the startup to complete, even if you start in the order of db-> web, the web may start before the db setup etc. is completed. In the meantime, accessing db from the web may result in an error. Here is a workaround ↓ https://docs.docker.com/compose/startup-order/

version: '3'
services:
    centos8:
        image: centos:centos8
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - ./hoge:/hoge
        depends_on:
            - centos8

Verification. Start in the order of centos8-> web. The opposite is true when deleting.

$ docker-compose up -d
Starting docker-compose-test_centos8_1 ... done
Starting docker-compose-test_web_1     ... done

//Delete
$ docker-compose down 
Stopping docker-compose-test_web_1 ... done
Removing docker-compose-test_web_1     ... done
Removing docker-compose-test_centos8_1 ... done
Removing network docker-compose-test_default

Reverse depends_on.

version: '3'
services:
    centos8:
        image: centos:centos8
        depends_on:
            - web
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - ./hoge:/hoge

Verification. Now started from the web.

docker-compose up -d
Starting docker-compose-test_web_1 ... done
Starting docker-compose-test_centos8_1 ... done

Use volumes

Use a shared volume between containers. ↓ For the time being, try creating a volume with docker-compose.

version: '3'
services:
    centos8:
        image: centos:centos8
        depends_on:
            - web
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - ./hoge:/hoge

volumes:
    hoge:

↓ volume is created.

$ docker-compose up -d 
Creating network "docker-compose-test_default" with the default driver
Creating volume "docker-compose-test_hoge" with default driver
Creating docker-compose-test_web_1 ... done
Creating docker-compose-test_centos8_1 ... done

$ docker volume ls
DRIVER    VOLUME NAME
local     docker-compose-test_hoge

Mount volume with service.

version: '3'
services:
    centos8:
        image: centos:centos8
        depends_on:
            - web
        volumes:
            - hoge:/hoge
    web:
        build: .
        ports:
            - 8000:80
        volumes:
            - hoge:/hoge

volumes:
    hoge:

Recommended Posts

try docker-compose
People using docker Try using docker-compose
Try HiveRunner
Try Mockito
Try Selenium
Try DbUnit
Command docker-compose
Try Lombok
Try running MySql and Blazor with docker-compose
Try App Clips
Try using libGDX
Docker-compose ~ volume trap ~
First try ~ catch
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try Java 8 Stream
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try Ruby Minitest
Roughly try Java 9
EC2 on Docker-compose