简体   繁体   中英

What is the difference between docker run -p and ports in docker-compose.yml?

I would like to use a standard way of running my docker containers. I have have been keeping a docker_run.sh file, but docker-compose.yml looks like a better choice. This seems to work great until I try to access my website running in the container. The ports don't seem to be set up correctly.

Using the following docker_run.sh , I can access the website at localhost . I expected the following docker-compose.yml file to have the same results when I use the docker-compose run web command.

docker_run.sh

docker build -t web .
docker run -it -v /home/<user>/git/www:/var/www -p 80:80/tcp -p 443:443/tcp -p 3316:3306/tcp web

docker-compose.yml

version: '3'
services:
    web:
        image: web
        build: .
        ports:
            - "80:80"
            - "443:443"
            - "3316:3306"
        volumes:
            - "../www:/var/www"

Further analysis

The ports are reported as the same in docker ps and docker-compose ps . Note: these were not up at the same time.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
<id>        web      "/usr/local/scripts/…"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:3307->3306/tcp   <name>
$ docker-compose ps
Name            Command               State                                Ports                              
---------------------------------------------------------------------------------------------------------------
web   /usr/local/scripts/start_s ...   Up      0.0.0.0:3316->3306/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

What am I missing?

As @richyen suggests in a comment, you want docker-compose up instead of docker-compose run .

docker-compose run ...

Runs a one-time command against a service.

That is, it's intended to run something like a debugging shell or a migration script, in the overall environment specified by the docker-compose.yml file, but not the standard command specified in the Dockerfile (or the override in the YAML file).

Critically to your question,

... docker-compose run [...] does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service's ports to be created and mapped to the host, specify the --service-ports flag.

Beyond that, the docker run command you show and the docker-compose.yml file should be essentially equivalent.

You don't run docker-compose.yamls the same way that you would run a local docker image that you have either installed or created on your machine. docker-compose files are typically launched running the command docker-compose up -d to run in detached mode. Then when you run docker ps you should see it running. You can also run docker-compose ps as you did above.

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