docker: Converting docker command into docker-compose syntax - docker

docker run --rm -it --name foo -h foo --net net1 ubuntu:latest /bin/bash
docker run --rm -it --name bar -h bar --net net1 ubuntu:latest /bin/bash
I wanted to ask how do I rewrite the command above in the docker-compose syntax?

By default docker-compose will create new bridge docker network for your setup. But if you have configured you docker network by yourself, you can specify it in your compose file, marking it as external network.
version: 3
services:
foo:
container_name: foo
image: ubuntu:latest
hostname: foo
networks:
- my_net
bar:
container_name: bar
image: ubuntu:latest
hostname: bar
networks:
- my_net
networks:
my_net:
external:
name: my_net

For
docker run --rm -it --name foo -h foo --net net1 ubuntu:latest /bin/bash
docker run --rm -it --name bar -h bar --net net1 ubuntu:latest /bin/bash
You will need to use something like below
version: 3
services:
foo:
image: ubuntu
hostname: foo
bar:
image: ubuntu
hostname: bar

Related

Starting docker containers

I have a docker-compose.yml file that starts two services: amazon/dynamodb-local on 8000 port and django-service. django-service runs tests that are dependent on dynamodb-local.
Here is working docker-compose.yml:
version: '3.8'
services:
dynamodb-local:
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
django-service:
depends_on:
- dynamodb-local
image: django-service
build:
dockerfile: Dockerfile
context: .
env_file:
- envs/tests.env
volumes:
- ./:/app
command: sh -c 'cd /app && pytest tests/integration/ -vv'
Now I need to run this without docker-compose, only using docker itself. I try to do following:
docker network create -d bridge net // create a network for dynamodb-local and django-service
docker run --network=net --rm -p 8000:8000 -d amazon/dynamodb-local:latest // run cont. att. to network
docker run --network=net --rm --env-file ./envs/tests.env -v `pwd`:/app django-service /bin/sh -c 'env && cd /app && pytest tests/integration -vv'
I can see that both services start, but I can't connect to the dynamo-db.
Where is the problem? Any comment or help is appreciated!
Through the docker-compose.yml, the amazon/dynamodb-local container has a name defined (container_name: dynamodb-local, If we do not set this property, docker-compose will use the service's name as container name). This enables other containers in the same network to address the container through its name.
In the docker-run command, we do not set an explicit container name. We can set an explicit container name by executing docker run ... --name dynamodb-local .... More details can be found in the corresponding docker run documentation.

What is the equivalent of ‍-h in docker-compose?

I want convert docker run to docker-compose with -h parameter
What is the equivalent of ‍‍‍‍-h in docker-compose?
My docker run command:
docker run --rm -p 8080:80/tcp -p 1935:1935 -p 3478:3478
-p 3478:3478/udp bigbluebutton -h webinar.mydomain.com
My docker-compose
version: "3"
services:
bigbluebutton:
build: .
container_name: "bigbluebutton"
restart: unless-stopped
ports:
- 1935:1935
- 3478:3478
- 3478:3478/udp
- 8080:80
networks:
public:
networks:
public:
external:
name: public
Anything that appears after the docker run image name is the Compose command:.
docker run \
--rm -p 8080:80/tcp -p 1935:1935 \ # Docker options
-p 3478:3478 -p 3478:3478/udp \ # More Docker options
bigbluebutton \ # Image name
-h webinar.mydomain.com # Command
services:
bigbluebutton:
build: .
command: -h webinar.mydomain.com
ports: ['8080:80', '1935:1935', '3478:3478', '3478:3478/udp']

Running Echoip Docker Image

I'm new to Docker and having trouble running the docker image https://github.com/mpolden/echoip#docker-image. What am I doing wrong? Any help would be greatly appreciated!
$ docker run mpolden/echoip -a ./GeoLite2-ASN.mmdb -c ./GeoLite2-City.mmdb -f ./GeoLite2-Country.mmdb
echoip: open ./GeoLite2-Country.mmdb: no such file or directory
The files are in the same directory. To test on your end, download the files: GeoLite2-ASN.mmdb, GeoLite2-City.mmdb, GeoLite2-Country.mmdb: https://gofile.io/d/G4i6hb
Having a docker-compose.yml would make this much easier to run:
version: "3.7"
services:
echoip:
image: mpolden/echoip
command: "echoip -a ./GeoLite2-ASN.mmdb -c ./GeoLite2-City.mmdb -f ./GeoLite2-Country.mmdb"
ports:
- "8080:8080"
restart: unless-stopped
The files are in the same directory
Docker containers cannot access the host filesystem unless it is mounted as a volume. For example, you could mount the current directory to /data in the container...
docker run --rm -v "${PWD}:/data" -p 8080:8080 mpolden/echoip \
-a /data/GeoLite2-ASN.mmdb \
-c /data/GeoLite2-City.mmdb \
-f /data/GeoLite2-Country.mmdb \
-l 0.0.0.0:8080
A Docker Compose config might look like this
version: "3.8"
services:
echoip:
image: mpolden/echoip
command: >
-l 0.0.0.0:8080
-a /data/GeoLite2-ASN.mmdb
-c /data/GeoLite2-City.mmdb
-f /data/GeoLite2-Country.mmdb
ports:
- "8080:8080"
volumes:
- "./:/data"
restart: unless-stopped

Can't communicate between docker containers in Gitlab-CI

In the second stage of my CI pipeline (after building a fresh docker image). I'm using other docker containers to test the new image, but I'm not able to get send any requests between these containers. My CI config is as follows:
stages:
- build
- test
services:
- docker:dind
variables:
# Enable network-per-build to allow gitlab services to see one another
FF_NETWORK_PER_BUILD: "true"
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: '/certs'
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
# IMAGE SETTINGS
NODE_ENV: "development"
API_URL: "http://danielgtaylor-apisprout:8000"
PORT: 8080
build:
stage: build
image: docker
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t ${CI_REGISTRY_IMAGE}:test .
- docker push ${CI_REGISTRY_IMAGE}:test
test:
stage: test
image: docker
services:
- docker:dind
- name: ${CI_REGISTRY_IMAGE}:test
alias: server
script:
- docker run --rm --name apisprout -d -v $CI_PROJECT_DIR/v2-spec.yml:/api.yaml danielgtaylor/apisprout /api.yaml
- docker run --rm --name newman -v $CI_PROJECT_DIR:/etc/newman postman/newman run 'Micros V2.postman_collection.json'
And receive the following error ENOTFOUND server server:8080
I have also tried with a new bridge network:
test:
stage: test
image: docker
services:
- docker:dind
script:
- docker network create -d bridge mynet
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker run -d --network=mynet --hostname=server ${CI_REGISTRY_IMAGE}:test
- docker run --rm --network=mynet --hostname=apisprout --name apisprout -d -v $CI_PROJECT_DIR/v2-spec.yml:/api.yaml danielgtaylor/apisprout /api.yaml
- docker run --rm --network=mynet --name newman -v $CI_PROJECT_DIR:/etc/newman postman/newman run 'Micros V2.postman_collection.json'
But I receive the same error ENOTFOUND server server:8080.
I am unable to run the docker run containers as services as I don't believe attaching volumes is supported yet.
I'm also running this on Gitlab.com, not a private runner.

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?
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
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.

Resources