1-I want to create image
2-copy a directory from host to image (/home/app)
3-give execute permission to sh file that locate in that image
4-run container
5-run sh file in that container
How can I do these steps in single dockerfile? or I should use docker compose?
You need a Dockerfile to build the docker image.
You need a docker-compose.yml file to run a container from the docker image if you want to do a docker-compose up or a docker stack deploy.
You can have the docker-compose.yml file build the image when you issue a docker-compose up by coding a build: statement in the docker-compose.yml file.
Example.
🐳 gforghetti@172.28.128.14:[~/test]] $ ls -la
total 24
drwxr-xr-x 5 gforghetti staff 160 Feb 5 09:16 .
drwxr-xr-x 52 gforghetti staff 1664 Feb 5 09:32 ..
-rw-r--r--@ 1 gforghetti staff 1226 Feb 5 09:28 Dockerfile
-rw-r--r-- 1 gforghetti staff 48 Feb 5 09:16 docker-compose.yml
-rw-r--r-- 1 gforghetti staff 39 Feb 5 09:15 test.sh
Dockerfile
🐳 gforghetti@172.28.128.14:[~/test] $ cat Dockerfile
FROM alpine:latest
ARG BUILDDIR="/home/app"
WORKDIR ${BUILDDIR}
#################################################################################################################################################
# Run the test.sh script when a container from this docker image is run.
#################################################################################################################################################
CMD /home/app/test.sh
#################################################################################################################################################
# Copy the test.sh script into the Build Directory.
#################################################################################################################################################
COPY ./test.sh ${BUILDDIR}
#################################################################################################################################################
# Change the permissions of the test.sh script to make it exwecuteable
#################################################################################################################################################
RUN chmod ugo+x ${BUILDDIR}/test.sh
🐳 gforghetti@172.28.128.14:[~/test] $ cat test.sh
#!/usr/bin/env sh
echo 'Hello World!'
Build and run the app with docker-compose
🐳 gforghetti@172.28.128.14:[~/test] $ docker-compose up
Unable to find image 'docker/compose:1.24.0-rc1' locally
1.24.0-rc1: Pulling from docker/compose
cd784148e348: Pull complete
6f08424c3bb2: Pull complete
5a77f8513f6c: Pull complete
ee96502984d5: Pull complete
Digest: sha256:3d2e6f08e1c4eeabfc0ae68aaa71b32086b780572a25155cdfe34b1025883958
Status: Downloaded newer image for docker/compose:1.24.0-rc1
Creating network "test_default" with the default driver
Building app
Step 1/6 : FROM alpine:latest
latest: Pulling from library/alpine
6c40cc604d8e: Pull complete
Digest: sha256:b3dbf31b77fd99d9c08f780ce6f5282aba076d70a513a8be859d8d3a4d0c92b8
Status: Downloaded newer image for alpine:latest
---> caf27325b298
Step 2/6 : ARG BUILDDIR="/home/app"
---> Running in 1f7a318a9d2e
Removing intermediate container 1f7a318a9d2e
---> 930378d8b50a
Step 3/6 : WORKDIR ${BUILDDIR}
---> Running in ee49d63eb90f
Removing intermediate container ee49d63eb90f
---> eac4d2cf8dd3
Step 4/6 : CMD /home/app/test.sh
---> Running in 2ba92d60277d
Removing intermediate container 2ba92d60277d
---> 0dd53e581e3f
Step 5/6 : COPY ./test.sh ${BUILDDIR}
---> 262e1aa80c30
Step 6/6 : RUN chmod ugo+x ${BUILDDIR}/test.sh
---> Running in 3b36f350e271
Removing intermediate container 3b36f350e271
---> e0d1acbc59ac
Successfully built e0d1acbc59ac
Successfully tagged test_app:latest
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating test_app_1 ... done
Attaching to test_app_1
app_1 | Hello World!
test_app_1 exited with code 0
Once the image is created you can also just do a docker container run.
🐳 gforghetti@172.28.128.14:[~/test] $ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test_app latest e0d1acbc59ac 5 minutes ago 5.53MB
alpine latest caf27325b298 5 days ago 5.53MB
🐳 gforghetti@172.28.128.14:[~/test] $ docker container run -it test_app:latest
Hello World!