How to convert a docker run -it bash command into a docker-compose?
Asked Answered
P

2

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?

Philbrook answered 12/2, 2019 at 13:9 Comment(0)
B
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
Bobstay answered 12/2, 2019 at 13:48 Comment(6)
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?Philbrook
@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.Bobstay
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.Philbrook
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.Bobstay
And remember to add detach flag to docker compose docker-compose up -d if you don't want the container to stop after execution.Bobstay
yeah, I know it. At the moment I'm starting it without -d because I want to see if it remains in execution or notPhilbrook
W
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.

Whether answered 12/2, 2019 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.