简体   繁体   中英

How do I run my docker container after loading it without running docker-compose up?

I have created a Dockerfile and docker-compose.yml file. I can run docker-compose up on Windows 10 which builds and then runs my Django web application. I can then successfully browse to 127.0.0.1:7000 and exercise my web application.

My question is, once I exit, how do fire up my image w/o running docker-compose up again?

When I type docker images I see two images: python and my_web_app . I've tried

docker run --publish 7000:8000 -- detach my_web_app

but it exits immediately.

My Dockerfile

FROM python:3.6
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN useradd user
USER user

My docker-compose.yml

version: "3"

services:
  web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      volumes:
        - .:/code
      ports:
        - "7000:8000"

In docker-compose.yml file, you have written build: . , which is using the Dockerfile at path . and creates an image named my_web_app.

You have 2 images because Python was used as a base image in the Dockerfile, so docker pulls that image first. And the other one is created as the resultant image (which contains python inside it).

Now when you run this command:

docker run --publish 7000:8000 -- detach my_web_app

It creates a container but will exit immediately because it has no command/process to run.

In docker-compose.yml , there exists command: python manage.py runserver 0.0.0.0:8000 which runs this command inside the container.

Two simple ways to run a new container are:

  •  docker run --publish 7000:8000 -- detach my_web_app python manage.py runserver 0.0.0.0:8000

which runs the specified command inside the container.

  • Or you can use docker exec -ti my_web_app and add the command to run after >this.

If you docker run my_web_app , it exits immediately because the Dockerfile doesn't have a CMD in it.

As others have answered, you can run docker-compose up to re-run the container. The docker-compose.yml file contains a number of settings that are needed to start the container. Only the docker-compose command reads this file; a plain docker run doesn't.

If you want to run this with a plain docker run command, you should do a couple of things:

  • Remove the command: line from the docker-compose.yml ; add the same CMD line to your Dockerfile . This makes the image have a default command that gets run, instead of needing to specify it every time you run the command.

  • Remove the volumes: from the docker-compose.yml ; you already ADD the application in your Dockerfile , so you don't want to overwrite it with random content from your host.

This would leave you with a simpler docker-compose.yml file

version: "3"
services:
  web:
      build: .
      image: my_web_app  # (override the name Compose picks)
      ports:
        - "7000:8000"

Once you have this, you can translate this to a docker run command

docker run \
  -d \
  -p 7000:8000 \   # ports:
  my_web_app       # image:

If you had other options ( environment: , volumes: , ...) you would need to translate this into docker run options. If you need to communicate between containers, Compose creates a default network for you and you would also need to recreate this.

That's how you run it. docker-compose up

Run docker-compose up -d if you want to leave it running in the background.

The Dockerfile generates the image, which is then used by docker-compose to build and run the service.

If you intend to run the service without running docker-compose , you should define how to execute the container in Dockerfile itself. Checkout cmd

CMD ["python", "manage.py", "runserver"]

You should probably move the command from the docker-compose file to the Dockerfile and build the image again using either docker-compose or docker build .

Once the image is available, just run using:

> docker run <image_name> --detach

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