Transform docker-compose.yml to docker run - docker

I'm newer in docker and I don't understand how to transform this docker-compose.yml :
version: '3'
services:
php:
build: .
volumes:
- "./:/var/www/html:ro"
- "./:/var/log/php"
Into a simple docker command in one line (if possible). I've tried this :
docker build -t mstp2html --mount source=./,target=/var/www/html,ro --mount source=./,target=/var/log/php -f- .
and this :
docker run --name mstp2html --mount source=./,target=/var/www/html,ro --mount source=./,target=/var/log/php ./Dockerfile
But don't work. What I've missing ?

You are mixing two concepts:
build: the stage in which the docker image is created, but it does not have relation on how it will run
run: the stage in which you run a docker container based on an existing docker image. In this stage, you can define options such as volumes.
In order for your example to work, you would need to split that into two stages:
Build the docker image:
docker build -t mstp2html_image .
Run a container from the image and mount the volumes you want to:
docker run --name mstp2html --mount type=bind,source="$(pwd)",target=/var/www/html,ro --mount type=bind,source="$(pwd)",target=/var/log/php mstp2html_image

Related

Docker: How to use "docker/compose" container?

I want to run Docker Compose inside a Docker container using the official docker/compose container.
My Dockerfile looks like this:
FROM docker/compose:latest
WORKDIR /
COPY ./docker-compose.yml .
COPY ./.env .
CMD [ "docker-compose", "up"]
Running docker build -t my-container . works. But running docker run --privileged my-container fails with:
> Couldn't connect to Docker daemon at http+docker://localhost - is it running?
>
> If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
What am I doing wrong? Do I have to specify DOCKER_HOST, and if yes, to what?
I guess the image docker/compose is used to in order to build docker-compose tool not to launch docker inside.
It's designed to be used by someone who would edit docker-compose source code.
However you could use the dind image (docker in docker).
Try installing docker-compose within dind (docker-in-docker), like so:
FROM docker:20.10-dind
RUN apk add docker-compose
WORKDIR /
COPY ./docker-compose.yml .
COPY ./.env .
CMD [ "docker-compose", "up"]
Try docker run -v /var/run/docker.sock:/var/run/docker.sock -it container_name
It will share docker socket.

How to declare two jobs with different Docker images in gitlab-ci.yml

Is there a way to run two jobs using two different Docker images?
I've tried to run this configuration but without any success:
before_script:
- docker info
build:default:
image: ubuntu:latest
script:
- pip3 install -r requirements.txt
- [..]
build:docker:
image: docker:latest
script:
- docker build -t app .
- docker run -d -p 8000:8000 --rm app:latest
- [..]
As mentioned by others in the comments the config file was fine, the problem was with gitlab runner itself.
So i made the following changes:
Change volume value to volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"] into /etc/gitlab-runner/config.toml
Add variables:
DOCKER_DRIVER: overlay
to your Dockerfile.

Run docker-compose without installation

I try to run docker-compose without installation, so using docker:compose repository (with docker run).
So I tried this way :
docker run -ti --rm -v $PWD:/XXX docker/compose:1.24.1 up -d
The problem is that I don't know the container dir name of docker/compose (here XXX) to mount my current folder as volume.
Any ideas...?
Thanks !
You can bind mount your local docker-compose.yaml to any place just remember to tell docker-compose use -f, like next:
docker run -ti --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/code docker/compose:1.24.1 -f /code/docker-compose.yaml up -d
Meanwhile, don't forget to add docker.sock of your host machine bind mount to the container.
That XXX folder can be anything inside the container. Basically in -v option of docker run. Its -v [host-directory]:[container-directory].
If you are trying to run a docker-compose up inside the container, then follow these steps:
Create a directory on host mkdir /root/test
Create docker-compose.yaml file with following contents:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis
Run docker run command to run docker-compose inside the container.
docker run -itd -v /var/run/docker.sock:/var/run/docker.sock -v /root/test/:/var/tmp/ docker/compose:1.24.1 -f /var/tmp/docker-compose.yaml up -d
NOTE: Here /var/tmp directory inside the container will contain docker-compose.yaml file so I have used -f option to specify complete path of the yaml file.
Hope this helps.

Launching docker command from docker-compose v.3 file

I'm learning about Docker and I'm at first steps.
I've to 'refresh' postgres image from compose file to initialize db scripts as YOSIFKIT here do through shell (https://github.com/docker-library/postgres/issues/193).
here is my Docker file:
FROM postgres:9.6.7
COPY docker-postgresql-9.6.7/prova.sql /docker-entrypoint-initdb.d/
and here is my compose file:
version: '3'
services:
postgresql_rdbms:
restart: always
image: postgres-prova
build:
context: ../
dockerfile: docker-postgresql-9.6.7/Dockerfile
command: bash -c "docker run -it --rm postgres-prova ls -ln /docker-entrypoint-initdb.d && docker run -it --rm postgres-prova && postgres"
environment:
PG_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- /srv/docker/postgresql:/var/lib/postgresql
HOW can I insert a command in a compose-file to do "docker run -it --rm imageToReload" ???
Because I've seen that "command:" in compose file works inside the container, but I want operate ON the container, on a upper level (=manage the container from the compose file, after the container creation)
Thank you very much
From what I understand you want docker-compose to delete/remove the container after every run so that the build is run each time and a fresh prova.sql file can be copied into the image each time the service is brought up. The --force-recreate flag is probably what you need.
The command directive within the yaml file provides the command that is run inside the container.

docker-compose up -d <name>; No such service: <name>

I have built an image using my dockerfile with the following command:
docker build –t <name>:0.2.34 .
and then I have tried to use my docker-compose.yml:
strat:
container_name: <name>
image: <name>:0.2.34
restart: always
command: bash -lc 'blah'
to bring up my container:
docker-compose up -d <name>
Which gives me the following 'error':
No such service: <name>
You should run: docker-compose up -d strat
From the documentation:
Usage: up [options] [SERVICE...]
You need to specify your service name, not your image name.
Note: You can simply run docker-compose up -d to start all the services that are in your docker-compose file.
docker run --name <name>:0.2.34
This will run your built image

Resources