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?

Alessandro C
  • 3,310
  • 9
  • 46
  • 82

2 Answers2

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
Raoslaw Szamszur
  • 1,723
  • 12
  • 21
  • 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? – Alessandro C Feb 12 '19 at 14:18
  • @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. – Raoslaw Szamszur Feb 12 '19 at 14:38
  • 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. – Alessandro C Feb 12 '19 at 15:04
  • 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: ..`](https://docs.docker.com/compose/compose-file/#command) overrides the default container entrypoint command. – Raoslaw Szamszur Feb 12 '19 at 15:09
  • And remember to add detach flag to docker compose `docker-compose up -d` if you don't want the container to stop after execution. – Raoslaw Szamszur Feb 12 '19 at 15:14
  • yeah, I know it. At the moment I'm starting it without -d because I want to see if it remains in execution or not – Alessandro C Feb 12 '19 at 15:15
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.

David Maze
  • 130,717
  • 29
  • 175
  • 215