Skip to content

Commit 454f137

Browse files
Dockerfile update (#707)
* Dockerfile update * Dockerfile update * Dockerfile update * Dockerfile update * Dockerfile update
1 parent f290e11 commit 454f137

File tree

4 files changed

+104
-103
lines changed

4 files changed

+104
-103
lines changed

DOCKER-EXAMPLES.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Examples of using the Docker image
2+
3+
To run this as a Docker container, which includes JSNAPy and PyEZ, simply pull it from the Docker hub and run it. The following will pull the latest image and run it in an interactive ash shell.
4+
5+
docker run -it --rm juniper/pyez-ansible
6+
7+
Although, you'll probably want to bind mount a host directory (perhaps the directory containing your playbooks and associated files). The following will bind mount the current working directory and start the ash shell.
8+
9+
docker run -it --rm -v $PWD:/project juniper/pyez-ansible
10+
11+
You can also use the container as an executable to run your playbooks. Let's assume we have a typical playbook structure as below:
12+
13+
example
14+
|playbook.yml
15+
|hosts
16+
|-vars
17+
|-templates
18+
|-scripts
19+
20+
We can move to the example directory and run the playbook with the following command:
21+
22+
cd example/
23+
docker run -it --rm -v $PWD:/playbooks juniper/pyez-ansible ansible-playbook -i hosts playbook.yml
24+
25+
You can pass any valid command string after the container name and it will be passed to Bash for execution.
26+
27+
You may have noticed that the base command is almost always the same. We can also use an alias to save some keystrokes.
28+
29+
alias pb-ansible="docker run -it --rm -v $PWD:/project juniper/pyez-ansible ansible-playbook"
30+
pb-ansible -i hosts playbook.yml
31+
32+
### Extending the container with additional packages
33+
34+
It's possible to install additional OS (Alpine) packages, Python packages (via pip), and Ansible collections at container instantiation. This can be done by passing in environment variables or binding mount files.
35+
36+
#### OS Packages
37+
38+
Environment Variable: `$APK`
39+
Bind Mount: `/extras/apk.txt`
40+
File Format: list of valid Alpine packages, one per line
41+
Examples:
42+
43+
As an environment variable, where the file containing a list of packages is in the current directory.
44+
45+
docker run -it --rm -v $PWD:/project -e APK="apk.txt" juniper/pyez-ansible
46+
47+
As a bind mount.
48+
49+
docker run -it --rm -v $PWD/apk.txt:/extras/apk.txt juniper/pyez-ansible
50+
51+
#### Python Packages
52+
53+
Environment Variable: `$REQ`
54+
Bind Mount: `/extras/requirements.txt`
55+
File Format: pip [requirements](https://pip.pypa.io/en/stable/reference/requirements-file-format/) file
56+
57+
Examples:
58+
59+
docker run -it --rm -v $PWD:/project -e REQ="requirements.txt" juniper/pyez-ansible
60+
61+
As a bind mount.
62+
63+
docker run -it --rm -v $PWD/requirements.txt:/extras/requirements.txt juniper/pyez-ansible
64+
65+
#### Ansible Packages
66+
67+
Environment Variable: `$COLLECTIONS`
68+
Bind Mount: `/extras/requirements.yml`
69+
File Format: Ansible [requirements](https://docs.ansible.com/ansible/devel/user_guide/collections_using.html#install-multiple-collections-with-a-requirements-file) file
70+
71+
72+
Examples:
73+
74+
docker run -it --rm -v $PWD:/project -e REQ="requirements.yml" juniper/pyez-ansible
75+
76+
As a bind mount.
77+
78+
docker run -it --rm -v $PWD/requirements.txt:/extras/requirements.yml juniper/pyez-ansible

Dockerfile

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
FROM juniper/pyez:2.5.3
1+
FROM python:3.12-alpine
22

33
LABEL net.juniper.image.maintainer="Juniper Networks <[email protected]>" \
44
net.juniper.image.description="Lightweight image with Ansible and the Junos roles"
55

6-
RUN apk add --no-cache build-base python3-dev py3-pip \
7-
openssl-dev curl ca-certificates bash openssh-client
8-
96
WORKDIR /tmp
10-
COPY requirements.txt .
11-
RUN pip3 install -r requirements.txt
127

13-
RUN apk del -r --purge gcc make g++ &&\
14-
rm -rf /source/* &&\
15-
rm -rf /var/cache/apk/* &&\
16-
rm -rf /tmp/*
8+
## Copy project inside the containers
9+
ADD requirements.txt .
10+
ADD entrypoint.sh /usr/local/bin/.
11+
12+
## Install dependencies and PyEZ
13+
RUN apk add --no-cache build-base python3-dev py3-pip \
14+
libxslt-dev libxml2-dev libffi-dev openssl-dev curl \
15+
ca-certificates py3-pip bash openssh-client
1716

18-
WORKDIR /usr/share/ansible/collections/ansible_collections/
19-
COPY ansible_collections/ .
17+
RUN pip install --upgrade pip \
18+
&& python3 -m pip install -r requirements.txt
2019

21-
WORKDIR /usr/bin
22-
COPY entrypoint.sh .
23-
RUN chmod +x entrypoint.sh
20+
# Also install the collections juniper.device
21+
# Install Ansible modules in one layer
22+
RUN ansible-galaxy collection install juniper.device
2423

25-
# Also install the roles, until collections is ready for prime-time
26-
RUN ansible-galaxy role install Juniper.junos,2.4.3
24+
## Clean up and start init
25+
RUN apk del -r --purge gcc make g++ \
26+
&& rm -rf /var/cache/apk/* \
27+
&& rm -rf /tmp/* \
28+
&& rm -rf /root/.cache \
29+
&& chmod +x /usr/local/bin/entrypoint.sh
2730

2831
WORKDIR /project
2932

3033
VOLUME /project
3134

32-
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
35+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

README.md

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -120,86 +120,6 @@ This will set your `$ANSIBLE_LIBRARY` variable to the repo location and the inst
120120
$ echo $ANSIBLE_LIBRARY
121121
/home/jeremy/Ansible/ansible-junos-stdlib/library:/usr/share/ansible
122122

123-
### Docker
124-
125-
To run this as a Docker container, which includes JSNAPy and PyEZ, simply pull it from the Docker hub and run it. The following will pull the latest image and run it in an interactive ash shell.
126-
127-
docker run -it --rm juniper/pyez-ansible
128-
129-
Although, you'll probably want to bind mount a host directory (perhaps the directory containing your playbooks and associated files). The following will bind mount the current working directory and start the ash shell.
130-
131-
docker run -it --rm -v $PWD:/project juniper/pyez-ansible
132-
133-
You can also use the container as an executable to run your playbooks. Let's assume we have a typical playbook structure as below:
134-
135-
example
136-
|playbook.yml
137-
|hosts
138-
|-vars
139-
|-templates
140-
|-scripts
141-
142-
We can move to the example directory and run the playbook with the following command:
143-
144-
cd example/
145-
docker run -it --rm -v $PWD:/playbooks juniper/pyez-ansible ansible-playbook -i hosts playbook.yml
146-
147-
You can pass any valid command string after the container name and it will be passed to Bash for execution.
148-
149-
You may have noticed that the base command is almost always the same. We can also use an alias to save some keystrokes.
150-
151-
alias pb-ansible="docker run -it --rm -v $PWD:/project juniper/pyez-ansible ansible-playbook"
152-
pb-ansible -i hosts playbook.yml
153-
154-
### Extending the container with additional packages
155-
156-
It's possible to install additional OS (Alpine) packages, Python packages (via pip), and Ansible collections at container instantiation. This can be done by passing in environment variables or binding mount files.
157-
158-
#### OS Packages
159-
160-
Environment Variable: `$APK`
161-
Bind Mount: `/extras/apk.txt`
162-
File Format: list of valid Alpine packages, one per line
163-
Examples:
164-
165-
As an environment variable, where the file containing a list of packages is in the current directory.
166-
167-
docker run -it --rm -v $PWD:/project -e APK="apk.txt" juniper/pyez-ansible
168-
169-
As a bind mount.
170-
171-
docker run -it --rm -v $PWD/apk.txt:/extras/apk.txt juniper/pyez-ansible
172-
173-
#### Python Packages
174-
175-
Environment Variable: `$REQ`
176-
Bind Mount: `/extras/requirements.txt`
177-
File Format: pip [requirements](https://pip.pypa.io/en/stable/reference/requirements-file-format/) file
178-
179-
Examples:
180-
181-
docker run -it --rm -v $PWD:/project -e REQ="requirements.txt" juniper/pyez-ansible
182-
183-
As a bind mount.
184-
185-
docker run -it --rm -v $PWD/requirements.txt:/extras/requirements.txt juniper/pyez-ansible
186-
187-
#### Ansible Packages
188-
189-
Environment Variable: `$ROLES`
190-
Bind Mount: `/extras/requirements.yml`
191-
File Format: Ansible [requirements](https://docs.ansible.com/ansible/devel/user_guide/collections_using.html#install-multiple-collections-with-a-requirements-file) file
192-
193-
_NOTE:_ This works for collections as well as roles.
194-
195-
Examples:
196-
197-
docker run -it --rm -v $PWD:/project -e REQ="requirements.yml" juniper/pyez-ansible
198-
199-
As a bind mount.
200-
201-
docker run -it --rm -v $PWD/requirements.txt:/extras/requirements.yml juniper/pyez-ansible
202-
203123
## Example Playbook
204124

205125
This example outlines how to use Ansible to install or upgrade the software image on a device running Junos OS.

entrypoint.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function pip_install {
1818
}
1919

2020
function galaxy_install {
21-
echo "Install Ansible roles"
21+
echo "Install Ansible Collections"
2222
ansible-galaxy install -r "$1"
2323
}
2424

@@ -38,17 +38,17 @@ elif [ -f "/extras/requirements.txt" ];then REQ="/extras/requirements.txt"
3838
else REQ=''
3939
fi
4040

41-
if [ "$ROLES" ]; then ROLES=$ROLES
42-
elif [ -f "/extras/requirements.yml" ]; then ROLES="/extras/requirements.yml"
43-
else ROLES=''
41+
if [ "$COLLECTIONS" ]; then COLLECTIONS=$COLLECTIONS
42+
elif [ -f "/extras/requirements.yml" ]; then COLLECTIONS="/extras/requirements.yml"
43+
else COLLECTIONS=''
4444
fi
4545

4646

4747
[[ -z "$APK" ]] || apk_add "$APK"
4848

4949
[[ -z "$REQ" ]] || pip_install "$REQ"
5050

51-
[[ -z "$ROLES" ]] || galaxy_install "$ROLES"
51+
[[ -z "$COLLECTIONS" ]] || galaxy_install "$COLLECTIONS"
5252

5353
if [ -z "$1" ]
5454
then

0 commit comments

Comments
 (0)