简体   繁体   中英

Container works when using Dockerfile, but does not work when using docker-compose.yml

What I would like to do

I am attempting to create a development environment using Docker Compose.

I am going to create the following containers:

  • python
  • apache
  • mysql

I'll start first working on getting the python container working.

My problem

My python container stays running when I use just a Dockerfile . However, when I use a docker-compose.yml file, this same container exits right away. I want it to stay running. What am I doing wrong? Thanks in advance.

app(develop) $ docker --version
Docker version 20.10.3, build 48d30b5

app(develop) $ docker-compose --version
docker-compose version 1.28.2, build 67630359

Dockerfile

I have started by creating a Dockerfile.python

# Set base image (host OS)
FROM python:3.9-slim-buster

# Run apt update and upgrade and install as necessary.
RUN apt update; \
    apt upgrade -y --no-install-recommends; \
    apt install python3-pip -y; \
    apt autoremove -y; \
    rm -vfr /var/lib/apt/lists/*

# Set the working directory in the container.
RUN mkdir -v /code
WORKDIR /code

# Command to run on container start
CMD ["/bin/bash"]

I am able to successfully build the container.

app(develop) $ ls -laR
total 32
drwxrwxr-x 5 mike mike 4096 Feb  3 22:18 .
drwxrwxr-x 4 mike mike 4096 Feb  1 13:51 ..
-rw-rw-r-- 1 mike mike 3357 Feb  3 22:08 docker-compose.yml
-rw-rw-r-- 1 mike mike 3070 Feb  3 22:18 Dockerfile.python
drwxrwxr-x 8 mike mike 4096 Feb  3 21:04 .git
drwxrwxr-x 2 mike mike 4096 Jan 31 15:01 src

./src:
total 16
drwxrwxr-x 2 mike mike 4096 Jan 31 15:01 .
drwxrwxr-x 5 mike mike 4096 Feb  3 22:18 ..
-rw-rw-r-- 1 mike mike  178 Jan 31 15:01 script.py


app(develop) $ docker build --tag my/python:0.1 \
> --file Dockerfile.python \
> .
blah, blah, blah
Successfully tagged my/python:0.1


app(develop) $ docker image ls
REPOSITORY         TAG                   IMAGE ID       CREATED              SIZE
my/python          0.1                   9ad1a86c619c   About a minute ago   510MB
python             3.9-slim-buster       677f7ac99e48   9 days ago           114MB

I am able to successfully create and start the container.

app(develop) $ docker run --name my-python \
> --mount 'type=bind,source=/home/mike/my-python-projects/app,destination=/code/' \
> --hostname=python-dev-cont \
> --interactive --tty --detach \
> my/python:0.1
3a29eaa6955dac3d1aaf6a27f80c2f6518fd788f2d0ed88a76797932842176ec


app(develop) $ docker container ls --all
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS          PORTS     NAMES
3a29eaa6955d   my/python:0.1   "/bin/bash"   13 seconds ago   Up 12 seconds             my-python


app(develop) $ docker exec --interactive --tty my-python /bin/bash
root@python-dev-cont:


root@python-dev-cont:/code# python --version
Python 3.9.1


root@python-dev-cont:/code# pip3 --version
pip 21.0 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)


root@python-dev-cont:/code# python src/script.py 
Hello World from Python!!!
x is: 4
That's all folks!


root@python-dev-cont:/code# exit
exit


app(develop) $ docker container stop my-python 
my-python


app(develop) $ docker container rm my-python 
my-python


app(develop) $ docker container ls --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

All is good so far.

docker-compose.yml

Since I will be having other containers as mentioned above, I will want to use Docker Compose so that the containers can work together.

Here is the code for docker-compose.yml

version: "3.8"

# =============================================================================
# docker container create
# =============================================================================
services:
# -----------------------------------------------------------------------------
  python:
# -----------------------------------------------------------------------------
    container_name: "my-python"
    
    build:
      context: "."
      dockerfile: "Dockerfile.python"

    image: "my/python:0.1"
    
    hostname: "python-dev-cont"
    
    volumes:
      - type: bind
        source: .
        target: /code/

When I use the following command, the container is created, starts, but then exits immediately.

app(develop) $ docker-compose up --build --detach
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
Building python
Sending build context to Docker daemon   80.9kB

Step 1/5 : FROM python:3.9-slim-buster
 ---> 677f7ac99e48
Step 2/5 : RUN apt update;     apt upgrade -y --no-install-recommends;     apt install python3-pip -y;     apt autoremove -y;     rm -vfr /var/lib/apt/lists/*
 ---> Using cache
 ---> 3be88973dbbc
Step 3/5 : RUN mkdir -v /code
 ---> Using cache
 ---> 91d715fef9d4
Step 4/5 : WORKDIR /code
 ---> Using cache
 ---> b746571615f7
Step 5/5 : CMD ["/bin/bash"]
 ---> Using cache
 ---> df09d2624564
Successfully built df09d2624564
Successfully tagged my/python:0.1
Creating my-python ... done


app(develop) $ docker container ls --all
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
b2dbaffe179d   my/python:0.1   "/bin/bash"   21 seconds ago   Exited (0) 20 seconds ago             my-python

I was expecting the container to be running as it was when I used just the Dockerfile.python . What am I doing wrong? I am using the same Dockerfile.python.

That's because your image define to run bash in command, which requires interactive mode.

you can add stdin_open + tty to prevent the container termination, but you'll still have to exec in order to get the command-line, as docker-compose stream out the logs.

version: "3.8"

# =============================================================================
# docker container create
# =============================================================================
services:
# -----------------------------------------------------------------------------
  python:
# -----------------------------------------------------------------------------
    container_name: "my-python"
    
    build:
      context: "."
      dockerfile: "Dockerfile.python"

    image: "my/python:0.1"
    
    hostname: "python-dev-cont"

    stdin_open: true

    tty: true
    
    volumes:
      - type: bind
        source: .
        target: /code/

You can also run with docker-compose run my-python

In your Dockerfile.python, you are using bin/bash in CMD. So when the image execution completes via docker-compose.yaml, container exits.

Instead you can automate the execution of the file you are putting inside container.

# Command to run on container start
CMD ["python","script.py"]

And anyhow you will have to exec into the container to check the logs etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM