简体   繁体   English

容器在使用 Dockerfile 时工作,但在使用 docker-compose.yml 时不工作

[英]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.我正在尝试使用 Docker Compose 创建开发环境。

I am going to create the following containers:我将创建以下容器:

  • python python
  • apache apache
  • mysql mysql

I'll start first working on getting the python container working.我将首先着手让python容器正常工作。

My problem我的问题

My python container stays running when I use just a Dockerfile .当我只使用Dockerfile时,我的 python 容器保持运行。 However, when I use a docker-compose.yml file, this same container exits right away.但是,当我使用docker-compose.yml文件时,同一个容器会立即退出。 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 Dockerfile

I have started by creating a Dockerfile.python我首先创建了一个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 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.由于我将拥有上述其他容器,因此我将希望使用 Docker Compose 以便容器可以一起工作。

Here is the code for docker-compose.yml这是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 .我期望容器能够像我只使用Dockerfile.python时那样运行。 What am I doing wrong?我究竟做错了什么? I am using the same Dockerfile.python.我正在使用相同的 Dockerfile.python。

That's because your image define to run bash in command, which requires interactive mode.那是因为您的图像定义为在命令中运行bash ,这需要交互模式。

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.您可以添加stdin_open + tty以防止容器终止,但您仍然必须exec才能获取命令行,如 docker-compose stream 日志。

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您也可以使用docker-compose run my-python

In your Dockerfile.python, you are using bin/bash in CMD.在您的 Dockerfile.python 中,您在 CMD 中使用 bin/bash。 So when the image execution completes via docker-compose.yaml, container exits.所以当镜像执行通过 docker-compose.yaml 完成时,容器退出。

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.无论如何,您必须执行到容器中以检查日志等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从主机到容器的Dockerfile COPY工作正常,但是当docker-compose.yml使用卷时,容器中的文件对主机不可见 - Dockerfile COPY from host to container works, but when docker-compose.yml uses volume, the file in container not visible to host 为什么在docker-compose.yml中使用多个env_files时变量替换不起作用? - Why does variables substitution not work when using multiple env_files in docker-compose.yml? docker-compose.yml 和 dockerfile 如何协同工作 - how does docker-compose.yml and dockerfile works together 代码在 Dockerfile 中有效,但在 docker-compose.yml 中无效 - Code works in Dockerfile but not in docker-compose.yml Web API端点可与docker-compose.yml一起使用,但不适用于仅使用Dockerfile进行构建和运行的情况 - Web API end point works with docker-compose.yml but not when I use only Dockerfile for build and run 与 docker-compose.yml 一起使用时未执行 Dockerfile 中的命令 - Commands in Dockerfile not executed while using with docker-compose.yml MacOS:如何使用 docker-compose.yml 和 Dockerfile 挂载卷? - MacOS: How to mount volumes using docker-compose.yml and Dockerfile? 未使用docker-compose.yml在Docker容器中设置环境变量 - Environment variables are not set in docker container using docker-compose.yml 带有 docker-compose.yml 的 Dockerfile - Dockerfile with docker-compose.yml 如何在不使用 Dockerfile 的情况下仅使用 Docker-compose.yml 文件启动 Node.js 应用程序的容器 - How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM