Alessandro C
Alessandro C

Reputation: 3560

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

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?

Upvotes: 5

Views: 2651

Answers (2)

David Maze
David Maze

Reputation: 158858

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.

Upvotes: 0

Raoslaw Szamszur
Raoslaw Szamszur

Reputation: 1740

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

Upvotes: 6

Related Questions