5

Given the following command:

docker run -dit -p 9080:9080 -p 9443:9443 -p 2809:2809 -p 9043:9043 --name container_name --net=host myimage:latest bash

How to convert it into an equivalent docker-compose.yml file?

2 Answers 2

6

In docker-compose in -it flags are being reflected by following:

tty: true
stdin_open: true

Equivalent to docker run --net=host is this:

services:
  web:
    ...
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

So your final docker-compose should look like this:

version: '3'
services:
  my_name:
    image: myimage:latest
    container_name: my_name
    ports:
     - "9080:9080"
     - "9443:9443"
     - "2809:2809"
     - "9043:9043"
    command: bash
    tty: true
    stdin_open: true
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

Compose file version 3 reference

Last but not least if you want to run it in the detached mode just add -d flag to docker-compose command:

docker-compose up -d
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I have tried to modify the command option in the docker-compose.yml, I have to start a specific service. But when I try docker-compose up, the container exit with code 0 even despite tty: true and stdin_open: true: what's going on?
@AlessandroC I'm not sure what you mean, exit code 0 is without errors so I assume it's ok. Moreover, stdin_open: true, tty: true won't actually give you a proper shell with up, because logs are being streamed from all the containers.
Probably the container remains in execution with the "bash" command, because it never stops. But if I use a different command, as soon the command finish its execution, the container stops.
It could be the case, tbh I just translated your docker run ... myimage:latest bash to docker compose, for me was a bit odd why you would want to run bash as container command but I've assumed it's just for example purposes. Basicaly command: .. overrides the default container entrypoint command.
And remember to add detach flag to docker compose docker-compose up -d if you don't want the container to stop after execution.
|
0

You can’t directly. Docker Compose will start up some number of containers that are expected to run more or less autonomously, and there’s no way to start typing commands into one of them. (What would you do if you had multiple containers that you wanted to start that were all just trying to launch interactive bash sessions?)

A better design would be to set up your Docker image so that its default CMD launched the actual command you were trying to run.

FROM some_base_image:x.y
COPY ...
CMD myapp.sh

Then you should be able to run

docker run -d \
    -p 9080:9080 \
    -p 9443:9443 \
    -p 2809:2809 \
    -p 9043:9043 \
    --name container_name \
    myimage:latest

and your application should start up on its own, successfully, with no user intervention. That’s something you can translate directly into Docker Compose syntax and it will work as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.